// pages/index/matchingdegree/matchingdegree.js

import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

import {
    GetXkjhbk, //列表
} from '../../../api/index'
import api from '../../../config/api';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataSourceArray: [{
                dictValue: "0",
                dictLabel: "专业匹配"
            },
            {
                dictValue: "1",
                dictLabel: "院校匹配"
            }
        ],
        tabSelect: 0,
        listArray: [],
        yearshow:false,
        yearArray:[{
            name: '2024年',
        }],
        show: false,
        actions: [{
                name: '全部',
            },
            {
                name: '本科',
            },
            {
                name: '专科(高职)'
            },
        ],
        pageNo:1,
        totalPages:0,
        nomore:false,
        searchString:'',
        queryType:'', // 本科，专科，全部传空
        isWuli:'0', //物理
        isLishi:'0',//历史
        isHuaxue:'0',//化学
        isShengwu:'0',//生物
        isZhengzhi:'0',//政治
        isDili:'0',//地理
        zyOrYxType:'zy', // 院校 或 专业
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.label != null) {
            let that = this
            let array = options.label.split(",");
            array.forEach((item)=>{
                if (item == '物理') {
                    that.setData({
                        isWuli:1
                    })
                } 
                if (item == '历史') {
                    that.setData({
                        isLishi:1
                    })
                } 
                if (item == '化学') {
                    that.setData({
                        isHuaxue:1
                    })
                } 
                if (item == '生物') {
                    that.setData({
                        isShengwu:1
                    })
                } 
                if (item == '政治') {
                    that.setData({
                        isZhengzhi:1
                    })
                } 
                if (item == '地理') {
                    that.setData({
                        isDili:1
                    })
                } 
            })
        }

        this._GetXkjhbk("?pageNum=" + this.data.pageNo + '&pageSize=20' + "&zyOrYxType=" + this.data.zyOrYxType + "&queryType=" + this.data.queryType + "&isWuli=" + this.data.isWuli + "&isLishi=" + this.data.isLishi + "&isHuaxue=" + this.data.isHuaxue + "&isShengwu=" + this.data.isShengwu + '&isZhengzhi=' + this.data.isZhengzhi + "&isDili=" + this.data.isDili + '&yxmc=' + this.data.searchString + '&zydhmc=' + this.data.searchString, true)
    },

    screenItemYearAction(){
        this.setData({
            yearshow:true
        })
    },

    // 专业层次点击事件
    screenItemAction() {
        this.setData({
            show:true
        })
    },
    onClose() {
        this.setData({ show: false,yearshow:false });
      },
    
      onSelect(event) {
        console.log(event.detail);
        this.onClose()
        if (event.detail.name == '2024年') {
            return
        }
        if (event.detail.name == '全部') {
            this.setData({
                queryType:''
            })
        }
        if (event.detail.name == '本科') {
            this.setData({
                queryType:'bk'
            })
        }
        if (event.detail.name == '专科(高职)') {
            this.setData({
                queryType:'zk'
            })
        }

        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this._GetXkjhbk("?pageNum=" + this.data.pageNo + '&pageSize=20' + "&zyOrYxType=" + this.data.zyOrYxType + "&queryType=" + this.data.queryType + "&isWuli=" + this.data.isWuli + "&isLishi=" + this.data.isLishi + "&isHuaxue=" + this.data.isHuaxue + "&isShengwu=" + this.data.isShengwu + '&isZhengzhi=' + this.data.isZhengzhi + "&isDili=" + this.data.isDili + '&yxmc=' + this.data.searchString + '&zydhmc=' + this.data.searchString, false)

      },

    // tab切换事件
    onChange(event) {
        let tabIndex = event.detail.name;
        console.log(tabIndex)
        this.setData({
            tabSelect: tabIndex,
        })
        if (tabIndex == 0) {
            this.setData({
                zyOrYxType:'zy'
            })
        } else {
            this.setData({
                zyOrYxType:'yx'
            })
        }
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this._GetXkjhbk("?pageNum=" + this.data.pageNo + '&pageSize=20' + "&zyOrYxType=" + this.data.zyOrYxType + "&queryType=" + this.data.queryType + "&isWuli=" + this.data.isWuli + "&isLishi=" + this.data.isLishi + "&isHuaxue=" + this.data.isHuaxue + "&isShengwu=" + this.data.isShengwu + '&isZhengzhi=' + this.data.isZhengzhi + "&isDili=" + this.data.isDili + '&yxmc=' + this.data.searchString + '&zydhmc=' + this.data.searchString, false)
    },

    // 输入框输入事件
  inputEvent: function(e) {
    // e.detail.value 包含输入框的当前值
    var value = e.detail.value;
    // 可以根据 field 来区分不同的输入框，这里只有一个输入框
    this.setData({
        searchString: value // 更新对应的data中的值
    });
  },
  // 搜索点击事件
  handleSearch: function(e) {

    this.setData({
        searchString: searchValue // 更新对应的data中的值
    });
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this._GetXkjhbk("?pageNum=" + this.data.pageNo + '&pageSize=20' + "&zyOrYxType=" + this.data.zyOrYxType + "&queryType=" + this.data.queryType + "&isWuli=" + this.data.isWuli + "&isLishi=" + this.data.isLishi + "&isHuaxue=" + this.data.isHuaxue + "&isShengwu=" + this.data.isShengwu + '&isZhengzhi=' + this.data.isZhengzhi + "&isDili=" + this.data.isDili + '&yxmc=' + this.data.searchString + '&zydhmc=' + this.data.searchString, false)
  },

    // 列表点击事件
    cellSelectAction(event) {
        console.log(event)
        let item = event.currentTarget.dataset
        let kmlb = ''
        console.log(item.kmlb)
        if (item.kmlb == "本科") {
            kmlb = "bk"
        } else {
            kmlb = "zk"
        }
        console.log(kmlb)
        if (this.data.tabSelect == 0) {
            wx.navigateTo({
                url: '/pages/index/professionSubjectResult/professionSubjectResult?zydh=' + item.zydh + "&zydhmc=" + item.zydhmc + "&queryType=" + kmlb,
            })
        } else {
            wx.navigateTo({
                url: '/pages/index/schoolSubjectResult/schoolSubjectResult?item=' + JSON.stringify(event.currentTarget.dataset.item) + "&queryType=" + kmlb
            })
        }
    },

    _GetXkjhbk(data, isLoading) {
        console.log(data)
        console.log(this.data.totalPages)
        console.log(this.data.listArray.length)
        let that = this
        if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
            this.setData({
              nomore: true
            })
            return;
          }
          GetXkjhbk(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                that.setData({
                    listArray: that.data.listArray.concat(array),
                    pageNo: that.data.pageNo + 1,
                    totalPages: res.total,
                  });
     
                  if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                        that.setData({
                            nomore: true
                        })
                  } 
            } else {
    
                Toast(res.msg)
            }
        })
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log('下拉刷新')
        // 显示顶部刷新图标
        wx.showNavigationBarLoading();
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this._GetXkjhbk("?pageNum=" + this.data.pageNo + '&pageSize=20' + "&zyOrYxType=" + this.data.zyOrYxType + "&queryType=" + this.data.queryType + "&isWuli=" + this.data.isWuli + "&isLishi=" + this.data.isLishi + "&isHuaxue=" + this.data.isHuaxue + "&isShengwu=" + this.data.isShengwu + '&isZhengzhi=' + this.data.isZhengzhi + "&isDili=" + this.data.isDili + '&yxmc=' + this.data.searchString + '&zydhmc=' + this.data.searchString, false)
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('上拉加载更多')
        this._GetXkjhbk("?pageNum=" + this.data.pageNo + '&pageSize=20' + "&zyOrYxType=" + this.data.zyOrYxType + "&queryType=" + this.data.queryType + "&isWuli=" + this.data.isWuli + "&isLishi=" + this.data.isLishi + "&isHuaxue=" + this.data.isHuaxue + "&isShengwu=" + this.data.isShengwu + '&isZhengzhi=' + this.data.isZhengzhi + "&isDili=" + this.data.isDili + '&yxmc=' + this.data.searchString + '&zydhmc=' + this.data.searchString, false)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
    
})